package it.univaq.mwt.log;

// LogManager scrive su disco i log del sistema

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class LogManager implements LivelloLog{
	
	private static LogManager singleton = new LogManager();
	private Properties properties = null;
	private File file = null;
	private FileWriter inputStream = null;
	private FileReader outputStream = null;
	
	private String nomeFile = null;
	/*
	 * 	Costruttore
	 */
	private LogManager(){
		properties = new Properties();
//		try {
//
//			InputStream in = new FileInputStream("config.proberties");
//			properties.load(in);
//			nomeFile = properties.getProperty("nomeFile");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}

		try {
			inputStream = new FileWriter("logModel.log", true);
			outputStream = new FileReader("logModel.log");
			file = new File("log.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}	// fine costruttore
	/*
	 * 	
	 */
	public static LogManager getIstance(){
		if (singleton == null){
			singleton = new LogManager();
		}
		return singleton;
	}

	
	// recupero data attuale
	public String dateNowToUsr(){
		Date today = new Date();
		String formatoData = "dd/MM/yyyy";
		SimpleDateFormat formatter = new SimpleDateFormat(formatoData);
		String dataOdierna = formatter.format(today);
		return dataOdierna;
	}
	// recupero ora attuale
	public String timeNowToUsr(){
		Date now = new Date();
		String formatoOra = "HH:mm:ss";
		SimpleDateFormat formatter = new SimpleDateFormat(formatoOra);
		String oraAttuale = formatter.format(now);
		return oraAttuale;
	}

	public void scrivi(String append, int msg) throws IOException {
		
			String inputString = null;
			switch (msg){
			case DEBUG:
				inputString = dateNowToUsr() + " " + timeNowToUsr() + " " + DEBUG_MSG + " " + " -> " + append	+ "\n";
				break;
			case RUN:
				inputString = dateNowToUsr() + " " + timeNowToUsr() + " " + RUN_MSG + " " + " -> " + append + "\n";
				break;
			case ERR:
				inputString = dateNowToUsr() + " " + timeNowToUsr() + " " + ERR_MSG + " " + " -> " + append + "\n";				
				break;
				default:
			}// switch
			
			inputStream = new FileWriter("logModel.log", true);
			
//			BufferedWriter out = new BufferedWriter(
//					inputStream); 
//			out.append(inputString); 
//			out.close(); 
			inputStream.append(inputString);
			inputStream.close();
		 
	}	
}